import { defineStore } from 'pinia'
import { reslogin } from '@/api/user/index.ts'
import type { LoginParams, LoginResult } from "@/api/user/usertype";
import { ref, computed } from 'vue'
import type { UserState } from './type.ts'
import { getToken, setToken } from '@/utils/token.ts'
import { constantRoutes } from '@/route/routes.ts'
import {RouteRecordRaw} from 'vue-router'
export const userStore = defineStore('user', () => {

  let token: any = ref((getToken() as string) || '')
  // 菜单路由
  let menuroutes: any = constantRoutes
  async function userLogin(longdata: LoginParams) {
    let res: LoginResult = await reslogin(longdata)
    if (res.code == 200) {
      token = (res.data.token as string)
      // 本地化存储
      setToken((res.data.token as string))
      return 'ok'
    } else {
      return Promise.reject(new Error(res.data.message))
    }
  }
  return {
    userLogin,
    token,
    menuroutes
  }
})